package com.okilo.online.app.event

/**
 * EventPublisher
 * @Author wwdeng
 * @CreateTime: 2024/10/16 10:51
 * @Annotation：发布者
 */
class OkiloEventPublisher {

    private val subscriberList = mutableSetOf<OkiloEventSubscriber>()

    /**
     * 注册订阅者
     */
    fun register(subscriber: OkiloEventSubscriber) {
        subscriberList.add(subscriber)
    }

    /**
     * 发布事件
     */
    fun publish(event: OkiloEventData) {
        subscriberList.forEach { it.onOkiloPublishEvent(event) }
    }

    /**
     * 移除订阅者
     */
    fun detach(subscriber: OkiloEventSubscriber) {
        subscriberList.remove(subscriber)
    }

    /**
     * 清空所有订阅者
     */
    fun clear() {
        subscriberList.clear()
    }

    /**
     * 获取当前订阅者数量
     */
    val subscriberCount: Int
        get() = subscriberList.size

    /**
     * 检查是否包含指定订阅者
     */
    fun contains(subscriber: OkiloEventSubscriber): Boolean {
        return subscriberList.contains(subscriber)
    }
}